#ifndef WEBSERVICECONFIG_H
#define WEBSERVICECONFIG_H



#include <QtCore>
#include <QtGui>
#include <QtGui>
#include <QtCore>
#include <QWidget>
#include <QPair>
#include <QTextDocument>
#include <QPair>
#include <QMap>
#include <QDateTime>
#include <QFile>
#include <QtCore> 
#include <QCoreApplication>
#include <iostream>
#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include <stdlib.h>
#include <QInputDialog>
#include <QMenu>
#include <QString>
#include <QtDebug>
#include <QDebug> 
#include <QDesktopServices>
#include <QDebug>
#include <QString>
#include <QMap>
#include <QList>
#include <QStringList>
#include <QObject>
#include <QDateTime>
#include <QDate>
#include <QImageReader>
#include <QPixmap>
#include <QSettings>
#include <QTimer>
#include <QProgressDialog>
#include <QPainter>
#include <QPixmap>
#include <QUrl>
#include <QColorDialog>
#include <QMessageBox>
#include <QWorkspace>
#include <QNetworkInterface>
#include <QDomDocument>
#include <QtDebug>
#include <QDebug> 
#include <QSettings>
#include <QDomDocument>
#include <QDomElement>
#include <QDomImplementation>
#include <QDomProcessingInstruction>
#include <QFile>
#include <QTextCodec>
#include <QString>
#include <QTextDocument>
#include <QTextCursor>
#include <QTextBrowser>
#include <QTextBlockFormat>
#include <QTextFrame>
#include <QTextTable>


#include "strutturaicone.h"

#include "FileFunction.h"

/* tag name */
#define _BODY_1_ \
         QString( "S:Body")  /* dopo root */
#define _BODY_2_ \
         QString( "ns2:getHouseResponse") /* dopo root 2 */ 
#define _BODY_3_ \
         QString( "return") /* dopo root 3 risultato  */
#define _TAG_ATTRIBUTI_ \
         QString( "attributeId") /* id dei attributi  */
#define _RADICE_ATTRIBUTI_ \
          QString( "attributes") /* root di attributi  */
#define _DATE_TAG_CALENDAR_ \
         QString( "dateRanges") /* dove cercare la data!!!  */
          
          
          
#define _NOTA_REGISTRO_CALENDARIO_ \
         QString( "Importato da Ticino Turismo.") /* dove cercare la data!!!  */
         
#define _WEBIDS_ \
         QString( "16") /* webserviceid page  */  

#define _WXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXPASS_ \
         QString( "ph4=!zz") /* webservicepassword  */
          
#define _WXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXUSER_ \
         QString( "PHohl") /* webserviceuser  */
         
         
         

         
         
#define _URLSYSTEM_ \
         QUrl("http://ws.ticino.ch/systemManager?wsdl")
         ///////////QUrl("http://wex.go/")
         /////////////////QUrl("http://ws.ticino.ch/systemManager?wsdl")
         
#define _URLHOUSES_ \
         QUrl("http://ws.ticino.ch/houseManager?wsdl")
         
#define _IMAGEWEBDIR_ \
         QString( "http://www.ticino.ch/pictures/alloggio/") /* imageurlpath  */  
         
         
#define WORK_TICINO_SESSION \
              QString( "%1/_sessioni_ticino_turismo/" ).arg( QDir::homePath())


#define _SAVE_SESSION_FILE_ \
         QString( "%1OldSession.dat").arg(WORK_TICINO_SESSION) /* webserviceuser  */

/* quote and dequote sql s result */
static inline QString SQLQuote( QString xml )
{
   //the following checks are necessary before exporting
  //strings to XML. see http://hdf.ncsa.uiuc.edu/HDF5/XML/xml_escape_chars.html for details
   QString text = xml;
   text.replace("\"","\\\"");  /* 228 ``*/
   text.replace("'","`");
   return text;
}


static inline bool Dircheck( const QString dirpath )
{
     QDir dira(dirpath);
    if ( dira.mkpath(dirpath) ) {
    return true;
    } else {
    return false;
    }
}

class IcoAttTi
{
    public:
    enum { MAGICNUMBER = 0xFFAAFFAA, VERSION = 1 };
    IcoAttTi() {
			it ="Senza descrizione";
			de ="Senza descrizione";
			fr ="Senza descrizione";
			en ="Senza descrizione";
			id = 0;
			Gid = 0;
			lastUpT = QDateTime::currentDateTime();
		}
    operator QVariant() const {
    return QVariant::fromValue(*this);
    }
    IcoAttTi& operator=( const IcoAttTi& d )
    {
      it = d.it;
      de = d.de;
      fr = d.fr;
      en = d.en;
      id = d.id;
      Gid = d.Gid;
      nameG = d.nameG;
      lastUpT = d.lastUpT;
      return *this;
    }
		
		/* vars permanent */
    QString nameG;
    QString it;
		QString de;
		QString fr;
		QString en;
    int id;
		int Gid;
		QDateTime lastUpT;
		QPixmap pic;
};


Q_DECLARE_METATYPE(IcoAttTi);

inline QDebug operator<<(QDebug debug, IcoAttTi& udoc)
{
	debug.nospace() << "IcoAttTi(name()" 
		<< udoc.lastUpT << ",creato()" 
	  << udoc.id << ","
    << udoc.nameG << ","
    << udoc.it << ","
    << udoc.de << ","
	  << udoc.fr << ","
	  << udoc.en << ","
    << udoc.Gid << ")";
	return debug.space();
}

inline QDataStream& operator<<(QDataStream& out, const IcoAttTi& udoc)
{
    out << udoc.it;
    out << udoc.de;
    out << udoc.fr;
    out << udoc.en;
    out << udoc.nameG;
    out << udoc.Gid;
    out << udoc.id;
	  out << udoc.pic;
    out << udoc.lastUpT;
    return out;
}

inline QDataStream& operator>>(QDataStream& in, IcoAttTi& udoc)
{
	  in >> udoc.it;
    in >> udoc.de;
    in >> udoc.fr;
    in >> udoc.en;
    in >> udoc.nameG;
    in >> udoc.Gid;
    in >> udoc.id;
	  in >> udoc.pic;
    in >> udoc.lastUpT;
    return in;
}

/* encode to name */
static inline QString toBase64( QString xml )
{
    QByteArray text;
    text.append(xml);
    return text.toBase64();
}



/* decode to name */
static inline QString fromBase64( QString xml )
{
    QByteArray xcode("");;
    xcode.append(xml);
    QByteArray precode(QByteArray::fromBase64(xcode));
    QString notetxt = precode.data();
    return notetxt;
}




static inline QString PhoneRegX( QString xml )
{
    
    if (xml.size() < 1) {
    return xml;
    }
    
    QString text = xml;
    QRegExp rx("(\\d+)");
    int pos = 0;
    QStringList list;
    QStringList resizer;
    while ((pos = rx.indexIn(xml, pos)) != -1) {
     list << rx.cap(1);
     pos += rx.matchedLength();
     }
     if (list.size() > 4) {
         for (int i = 0; i < 4; ++i) {
            resizer.append(list.at(i)); 
         }
         list = resizer;
     }
     if (list.size() == 4) {
        QString telo = list.at(0)+"/";
        list.removeFirst();
        telo.append(list.join(" "));
        return telo;
     } else {
      return list.join(" ");
     }
     /////////  044 391 43 89
}



static inline QDate DateFromUtc( QString indate )
{
    /* incomming format "2006-07-01T12:00:00+02:00" 
       2006-02-17
    */
           if (indate.size() < 9) {
           return QDate(1970,1,1);
           }
           
           int annoadesso = QDateTime::currentDateTime().date().year();
           const QString reformat = indate.left(10);
           qDebug() << "date  converted " << reformat;
           QDate DDp = QDate::fromString(reformat,"yyyy-MM-dd");
           if (DDp.year() < annoadesso ) {
            return QDate(1970,1,1);
           } else {
            return DDp;
           }

}














#endif // WEBSERVICECONFIG_H

